//
//  PrescriptionOrderLogisticsController.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2021/8/18.
//  Copyright © 2021 QDSG. All rights reserved.
//

import UIKit

class PrescriptionOrderLogisticsController: BaseViewController {
    
    private let viewModel = PrescriptionOrderLogisticsViewModel()
    
    private var expressNum: String = ""
    
    convenience init(with expressNum: String) {
        self.init()
        self.expressNum = expressNum
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        title = "查看物流"
        
        loadData()
    }
    
    override func makeUI() {
        super.makeUI()
        
        expressNoLabel.text = expressNum
        
        tableHeaderView.addSubview(expressNameLabel)
        expressNameLabel.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.centerY.equalToSuperview()
        }
        
        tableHeaderView.addSubview(expressNoLabel)
        expressNoLabel.snp.makeConstraints { make in
            make.right.equalTo(-16)
            make.centerY.equalToSuperview()
            make.left.greaterThanOrEqualTo(expressNameLabel.snp.right).offset(10).priority(.low)
        }
        
        tableView.tableHeaderView = tableHeaderView
        
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.edges.equalTo(view.safeAreaLayoutGuide).priority(.low)
            make.bottom.equalToSuperview()
        }
    }
    
    override func bindViewModel() {
        super.bindViewModel()
        
        viewModel.updateClosure = { [weak self] in
            self?.updateUI()
        }
    }
    
    private func updateUI() {
        expressNoLabel.text = expressNum
        
        tableView.reloadData()
    }
    
    private func loadData() {
        viewModel.getLogisticsInfo(expressNum: expressNum)
    }
    
    private lazy var tableHeaderView: UIView = {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: screenW, height: 60))
        view.backgroundColor = .white
        return view
    }()
    
    private lazy var expressNameLabel: UILabel = {
        let label = UILabel()
        label.textColor = .textDark
        label.text = "邮政EMS"
        label.font = .medium_14
        return label
    }()
    
    private lazy var expressNoLabel: UILabel = {
        let label = UILabel()
        label.textColor = .textLight
        label.font = .medium_14
        label.textAlignment = .right
        return label
    }()

    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .grouped)
        tableView.backgroundColor = .background
//        tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 0.001))
        tableView.tableFooterView = UIView()
        tableView.separatorStyle = .none
        
        tableView.delegate = self
        tableView.dataSource = self
        
        tableView.register(PrescriptionOrderLogisticsInfoCell.nibForCell(),
                           forCellReuseIdentifier: PrescriptionOrderLogisticsInfoCell.reuseIdentifier())
        
        return tableView
    }()

}

extension PrescriptionOrderLogisticsController: UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return viewModel.numberOfSections
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.numberOfRows(in: section)
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: PrescriptionOrderLogisticsInfoCell.reuseIdentifier(),
                                                 for: indexPath) as! PrescriptionOrderLogisticsInfoCell
        let viewModel = viewModel.viewModel(in: indexPath.section).viewModel(at: indexPath)
        cell.bind(to: viewModel)
        return cell
    }
}

extension PrescriptionOrderLogisticsController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = UINib.loadViewFrom(PrescriptionOrderLogisticsSectionHeader.self)
        header.bind(to: viewModel.viewModel(in: section), in: section)
        return header
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.001
    }
}
